﻿using System.Linq;
using ClientTools.Library.DataTypes.Umbraco;
using ClientTools.Library.DataTypes.Versions;

namespace ClientTools.Client.Library
{
    public static class ExtensionMethods
    {
        public static NodeCollection ToNodeCollection(this ClientToolsWebServices.Node[] serviceMedias)
        {
            var returnValue = new NodeCollection();

            foreach (var serviceMedia in serviceMedias)
                returnValue.Add(serviceMedia.ToNode());
                
            return returnValue;
        }

        public static Node ToNode(this ClientToolsWebServices.Node serviceNode)
        {
            var returnValue = new Node
                                  {
                                      Id = serviceNode.Id,
                                      Name = serviceNode.Name,
                                      NodeTypeAlias = serviceNode.NodeTypeAlias,
                                      NodeTypeId = serviceNode.NodeTypeId,
                                      HasChildren = serviceNode.HasChildren
                                  };
            foreach (var serviceProperty in serviceNode.Properties)
                returnValue.Properties.Add(serviceProperty.ToProperty());
            
            foreach (var serviceChildMedia in serviceNode.Children)
                returnValue.Children.Add(serviceChildMedia.ToNode());

            return returnValue;
        }

        private static Property ToProperty(this ClientToolsWebServices.Property serviceProperty)
        {
            return new Property
            {
                Id = serviceProperty.Id,
                PropertyTypeAlias = serviceProperty.PropertyTypeAlias,
                PropertyTypeId = serviceProperty.PropertyTypeId,
                Value = serviceProperty.Value
            };
        }

        public static VersionInfo ToVersionInfo(this ClientToolsWebServices.VersionInfo serviceVersionInfo)
        {
            return new VersionInfo
            {
                Documents = serviceVersionInfo.Documents,
                PropertyData = serviceVersionInfo.PropertyData,
                Versions = serviceVersionInfo.Versions
            };
        }
        
        public static VersionCollection ToVersionCollection(this ClientToolsWebServices.Version[] serviceVersions)
        {

            var vc = new VersionCollection();
            vc.AddRange((from x in serviceVersions
                         select
                             new Version
                                 {
                                     Guid = x.Guid,
                                     Id = x.Id,
                                     IsNewest = x.IsNewest,
                                     Published = x.Published,
                                     Text = x.Text,
                                     UpdateDate = x.UpdateDate,
                                     UserId = x.UserId,
                                     UserName = x.UserName,
                                     VersionDate = x.VersionDate
                                 }));
            return vc;
        }
    }
}
